package com.lifl.b328.garden.model;

import android.content.ContentValues;
import android.database.Cursor;

public class UserLocDistDAO extends CacheDAO<CoupleLocalDistant> {

	@Override
	public long add(CoupleLocalDistant obj) {
		ContentValues values = new ContentValues();
		values.put(Constantes.KEY_POOL_USER_LOCAL, obj.getLocal());
		values.put(Constantes.KEY_POOL_USER_DISTANT, obj.getDistant());
		
		return db.insert(Constantes.TABLE_POOL_USER, null, values);
	}

	@Override
	public long delete(long rowid) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public long edit(CoupleLocalDistant obj, long rowid) {
		ContentValues values = new ContentValues();
		values.put(Constantes.KEY_POOL_USER_LOCAL, obj.getLocal());
		values.put(Constantes.KEY_POOL_USER_DISTANT, obj.getDistant());
		return db.update(Constantes.TABLE_POOL_USER, values, Constantes.KEY_POOL_USER_ROWID + " = "+ rowid, null);
	}

	@Override
	public CoupleLocalDistant getByColumn(String column,Object valeur) {
		Cursor cursor = db.query(Constantes.TABLE_POOL_USER, null, column +" = "+((Long) valeur),null, null, null, null);
		System.out.println("valeur "+(Long)valeur);
		if(cursor != null){
			cursor.moveToFirst();
			if(cursor.getCount() == 0)
				return null;
		}
		long distant = cursor.getLong(cursor.getColumnIndex(Constantes.KEY_POOL_USER_DISTANT));
		long local = cursor.getLong(cursor.getColumnIndex(Constantes.KEY_POOL_USER_LOCAL));
		return new CoupleLocalDistant(local, distant);
		
	}

	@Override
	public CoupleLocalDistant getById(long id) {
		Cursor cursor = db.query(Constantes.TABLE_POOL_USER, null, Constantes.KEY_POOL_USER_LOCAL +" = " + id,
				null, null, null, null);
		if(cursor != null)
		{
			if (cursor.getCount() == 0)
				return null;
			cursor.moveToFirst();
		}
		return new CoupleLocalDistant(cursor.getLong(cursor.getColumnIndex(Constantes.KEY_POOL_USER_LOCAL)),
				cursor.getLong(cursor.getColumnIndex(Constantes.KEY_POOL_USER_DISTANT)));
	}

	@Override
	public Cursor getAll(long id) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Cursor getAll(long id, String order) {
		// TODO Auto-generated method stub
		return null;
	}

	

}
